package com.ftdigital.primemovers_operators.adpter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.model.AddOilHistory;
import com.ftdigital.primemovers_operators.model.AddOilUnfinishHistory;
import com.ftdigital.primemovers_operators.model.OilReprotItme;

import org.w3c.dom.Text;


/**
 * Created by Curry on 2015-12-16.
 * 正在交易中
 */
public class OngoingListViewAdapter extends RecyclerView.Adapter<OngoingListViewAdapter.ViewHolder>
        implements View.OnClickListener {

    private Context mContext;
    private AddOilUnfinishHistory addOilUnfinishHistory;
    public OngoingListViewAdapter(Context context, AddOilUnfinishHistory addOilUnfinishHistory) {
        mContext = context;
        this.addOilUnfinishHistory = addOilUnfinishHistory;
    }


    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , int position);
    }

    @Override
    public OngoingListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ongoing_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OngoingListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);

        holder.companyAndlicenecNum.setText(addOilUnfinishHistory.getData().get(position).getCompany_name()
                + "(" + addOilUnfinishHistory.getData().get(position).getCar_num() + ")");
        holder.typeAndNum.setText(addOilUnfinishHistory.getData().get(position).getTime()
                + " " + addOilUnfinishHistory.getData().get(position).getQty());
        holder.price.setText(addOilUnfinishHistory.getData().get(position).getPrice()+"");
    }

    @Override
    public int getItemCount() {
        return addOilUnfinishHistory.getData().size();
    }



    class ViewHolder extends RecyclerView.ViewHolder{
        private RelativeLayout layout;
        private TextView companyAndlicenecNum; //货运公司和车牌
        private TextView typeAndNum;//油品型号和加油数量
        private TextView price;//价格
        public ViewHolder(View itemView) {
            super(itemView);
            companyAndlicenecNum = (TextView) itemView.findViewById(R.id.company_content);
            typeAndNum = (TextView) itemView.findViewById(R.id.trading_info);
            price = (TextView) itemView.findViewById(R.id.price);
        }

        public RelativeLayout getLayout() {
            return layout;
        }

        public TextView getCompanyAndlicenecNum() {
            return companyAndlicenecNum;
        }

        public TextView getTypeAndNum() {
            return typeAndNum;
        }

        public TextView getPrice() {
            return price;
        }
    }
}
